package com.tianqu.tms.entity;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.CopyFile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.AttnDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.ViewListDao;
import com.tianqu.tms.dto.AttnDto;

/**
 * 经办人
 * 
 * @author Administrator
 * 
 */
public class Attn extends ViewListManager {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4289742917890016560L;
	private int id;
	private String attnId;
	private String memberId;//会员ID
	private String attnName;//经办人姓名
	private String attnIdCardNo;//经办人身份证号
	private String attnAddress;//经办人地址
	private String attnPhone;//经办人电话
	private String attnPhoto;//经办人照片
	private String userId;//登录ID
	private int insertUserId;//受理人/插入用户
	private Timestamp insertTime;//受理时间/插入时间
	private int updateUserId;//更新人
	private Timestamp updateTime;//
	private String accessCardId;//积分卡编号
	private String accessCardNo;//积分卡卡号
	private int cardScore;//累计积分	
	private String attnStatus;
	private int cssFlag;
	private int num;
	private double integralRatio;//积分比例
	private int virtualMargin;
	private int giftMargin;
	private int realMargin;
	private String shortName;
	private int recharge;
	/**
	 * 
	 */

	public static Attn getInstance() {
		Attn a = new Attn();
		a.setExisting(false);
		return a;
	}

	@SuppressWarnings("unchecked")
	public static List getAttnInfo() throws TmsDaoException {
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		List<AttnDto> dtoList = uDao.getAttnInfo();
		List list = new ArrayList();
		if (dtoList.size() > 0 && dtoList != null) {
			Map map = null;
			for (int i = 0; i < dtoList.size(); i++) {
				AttnDto dto = dtoList.get(i);
				map = new HashMap();
				map.put("key", dto.getAttnId());
				map.put("value", dto.getAttnName() + "(" + dto.getAttnId()
						+ ")");
				map.put("attnPhoto", dto.getAttnPhoto());
				map.put("attnName", dto.getAttnName());
				list.add(map);
			}
		}
		if (list.size() == 0) {
			return null;
		}
		return list;
	}
	/**
	 * @param attn
	 * @return
	 * @throws TmsDaoException
	 */
	public Attn getAttnInfoById(String attn) throws TmsDaoException
	{
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = uDao.getAttnInfoById(attn);
		Attn entity = new Attn();
		if(dto!=null)
		{
			entity.setAttnId(dto.getAttnId());
			entity.setUpdateUserId(dto.getUpdateUserId());
			entity.setAttnName(dto.getAttnName());
			entity.setAttnPhoto(dto.getAttnPhoto());
			entity.setAccessCardNo(dto.getAccessCardNo());
			entity.setCardScore(dto.getCardScore());
			return entity;
		}
		return null;
	}
	
	/**
	 * 经办人信息插入
	 * @param attn
	 * @throws TmsDaoException
	 */
	public void insertAttn(Attn attn,String sysPath)throws TmsDaoException{
		AttnDto dto = new AttnDto();
		//将image图片信息放到服务器上
		//对每个path进行处理   。。。【photo就是imgpath
		File outFile = null;
		File inFile = null;
		
		sysPath = sysPath.replace("/tmsweb", TmsConstants.BLANK);
		dto = entityToDto(attn);
		String attnId = attn.getAttnId();
		if(!StringUtil.isNullOrBlank(dto.getAttnPhoto())){
			inFile = new File(sysPath+dto.getAttnPhoto());
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_MEMBER_IMAGE_PATCH_KEY)+attnId+"/")+attnId+".jpg");
			CopyFile.copy(inFile, outFile);	
			dto.setAttnPhoto(imgPath(dto.getAttnPhoto(), attnId));
		}
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		uDao.insertAttn(dto);
	}

	/**
	 * 经办人修改
	 * @param attn
	 * @param sysPath
	 * @throws TmsDaoException
	 */
	public void updateAttn(Attn attn,String sysPath)throws TmsDaoException{
		AttnDto dto = new AttnDto();
		//将image图片信息放到服务器上
		//对每个path进行处理   。。。【photo就是imgpath
		File outFile = null;
		File inFile = null;
		
		sysPath = sysPath.replace("/tmsweb", TmsConstants.BLANK);
		dto = entityToDto(attn);
		String attnId = dto.getAttnId();
		if(!StringUtil.isNullOrBlank(dto.getAttnPhoto())){
			inFile = new File(sysPath+dto.getAttnPhoto());
			if(dto.getAttnPhoto().indexOf("carImageTmp") != -1)
			{
			outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_MEMBER_IMAGE_PATCH_KEY)+attnId+"/")+attnId+".jpg");			
			CopyFile.copy(inFile, outFile);	
			}
			dto.setAttnPhoto(imgPath(dto.getAttnPhoto(), attnId));
		}
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		uDao.updateAttn(dto);
	}
	/**
	 * 处理存入数据库中的图片路径
	 */
	private String imgPath(String path,String attnId){
		return ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOAD_MEMBER_IMAGE_PATCH_KEY) +attnId+"/"+attnId+".jpg");	
	}	
	/**
	 * dto -> entity
	 * @param attn
	 * @return
	 */
	private AttnDto entityToDto(Attn attn){
		AttnDto dto = new AttnDto();
		dto.setAttnId(attn.getAttnId());
		dto.setAttnName(attn.getAttnName());
		dto.setAttnAddress(attn.getAttnAddress());	
		dto.setAttnIdCardNo(attn.getAttnIdCardNo());
		dto.setAttnPhone(attn.getAttnPhone());
		dto.setMemberId(attn.getMemberId());
		dto.setUserId(attn.getUserId());
		dto.setInsertUserId(attn.getInsertUserId());
		dto.setUpdateUserId(attn.getUpdateUserId());
		dto.setAttnPhoto(attn.getAttnPhoto());
		dto.setAccessCardId(attn.getAccessCardId());
		dto.setAccessCardNo(attn.getAccessCardNo());
		return dto;
	}
	/**
	 * entity -> dto
	 * @param attn
	 * @return
	 */
	private Attn dtoToEntity(AttnDto dto,Attn entity){
		entity.setAttnId(dto.getAttnId());
		entity.setAttnName(dto.getAttnName());
		entity.setAttnAddress(dto.getAttnAddress());	
		entity.setAttnIdCardNo(dto.getAttnIdCardNo());
		entity.setAttnPhone(dto.getAttnPhone());
		entity.setMemberId(dto.getMemberId());
		entity.setUserId(dto.getUserId());
		entity.setInsertUserId(dto.getInsertUserId());
		entity.setUpdateUserId(dto.getUpdateUserId());
		entity.setAttnPhoto(dto.getAttnPhoto());
		entity.setAccessCardId(dto.getAccessCardId());
		entity.setAccessCardNo(dto.getAccessCardNo());
		entity.setCardScore(dto.getCardScore());
		entity.setIntegralRatio(dto.getIntegralRatio());
		entity.setVirtualMargin(dto.getVirtualMargin());
		entity.setRealMargin(dto.getRealMargin());
		entity.setShortName(dto.getShortName());
		return entity;
	}
	/**
	 * 积分管理
	 * @return
	 * @throws TmsDaoException 
	 */
	public Attn getIntegralManagementInfo(String accID) throws TmsDaoException
	{
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto = uDao.getIntegralManagementInfo(accID);
		Attn attn = null;
		if(dto!=null)
		{
			attn = new Attn();
			attn = dtoToEntity(dto, attn);
		}
		return attn;
	}
	
	/**
	 * 积分管理
	 * @return
	 * @throws TmsDaoException 
	 */
	public Attn getIntegralInfo(String accessCardNo) throws TmsDaoException
	{
		if(StringUtil.isNullOrBlank(accessCardNo)) return null;
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto = uDao.getIntegralInfo(accessCardNo);
		Attn attn = null;
		if(dto!=null)
		{
			attn = new Attn();
			attn = dtoToEntity(dto, attn);
		}
		return attn;
	}
	
	public void updateIntegralCardScore(Attn entity) throws TmsDaoException
	{
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto.setAttnId(entity.getAttnId());
		dto.setCardScore(entity.getCardScore());
		dto.setUpdateUserId(entity.getUpdateUserId());
		uDao.updateIntegralCardScore(dto);
	}
	
	/**
	 * 查看是否有相同的积分卡卡号
	 * @param accessCardNo
	 * @return
	 * @throws TmsDaoException
	 */
	public int findAttnByAccessCardNo(String accessCardNo) throws TmsDaoException{
		int flag = 1;
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		flag = uDao.findAttnByAccessCardNo(accessCardNo);
		return flag;
	}
	/**
	 * 判断是否有相同的积分卡编号
	 * @param accessCardId
	 * @return
	 * @throws TmsDaoException
	 */
	public int findAttnByAccessCardId(String accessCardId) throws TmsDaoException{
		int flag = 1;
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		flag = uDao.findAttnByAccessCardId(accessCardId);
		return flag;
	}
	
	/**
	 * 载入经办人列表
	 * @return
	 * @throws TmsDaoException
	 */
	public List<Attn> findAllAttnInfo() throws TmsDaoException{
		List<AttnDto> attnDtoList = new ArrayList<AttnDto>();
		List<Attn> attnList = new ArrayList<Attn>();
		AttnDao aDao = (AttnDao)TmsDao.getInstance(TmsDao.TMS_ATTN,
				getPageNo(), getMaxIndex(), getSortKey(), isAsc());
		ViewListDao idao = (ViewListDao) TmsDao.getViewListDao((TmsDao) aDao,
				TmsDao.TMS_ATTN); 
		attnDtoList = aDao.findAttnList();
		int i = 1;
		if(attnDtoList != null){
			for(AttnDto dto : attnDtoList){
				Attn attn = new Attn();
				if (dto.getAttStatus() == 1) {
					// 正常 删除 禁用
					attn.setAttnStatus("正常");
				}
				if (dto.getAttStatus() == 2) {
					attn.setAttnStatus("删除");
				}
				if (dto.getAttStatus() == 3) {
					attn.setAttnStatus("禁用");
				}
				if(i%2 == 0){
					attn.setCssFlag(1);
				}else{
					attn.setCssFlag(2);
				}
				attn.setNum(i);
				Member mem = Member.getInstance();
				attn.setMemberId(mem.getMemNameByMemId(Integer.parseInt(dto.getMemberId())));
				attn.setAttnId(dto.getAttnId());
				attn.setUserId(dto.getUserId());
				attn.setAttnName(dto.getAttnName());
				attn.setAttnIdCardNo(dto.getAttnIdCardNo());
				attn.setAttnPhone(dto.getAttnPhone());				
				attn.setAttnPhoto(dto.getAttnPhoto());
				attnList.add(attn);
				
				i++;
			}
		}
		
		setPagingInfo(idao);
		return attnList;
	}
	
	public Map<String, AttnDto> findAllAttn() throws TmsDaoException{
		AttnDao aDao = (AttnDao)TmsDao.getInstance(TmsDao.TMS_ATTN);
		List<AttnDto> list = aDao.findAllAttn();
		Map<String, AttnDto> map = new HashMap<String, AttnDto>();
		for (AttnDto attnDto : list) map.put(attnDto.getAttnId(), attnDto);
		return map;
	}
	
	public void updateMarginInfo(Attn entity) throws TmsDaoException
	{
		AttnDao mDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto.setVirtualMargin(entity.getVirtualMargin());
		dto.setRealMargin(entity.getRealMargin());
		dto.setGiftMargin(entity.getGiftMargin());
		dto.setUpdateUserId(entity.getUpdateUserId());
		dto.setAccessCardNo(entity.getAccessCardNo());
		mDao.updateAttnMarginInfo(dto);
	}
	/**
	 * 积分卡更新
	 * @param entity
	 * @throws TmsDaoException 
	 */
	public void updateMarginInfoByAccessCardNo(Attn entity) throws TmsDaoException
	{
		AttnDao mDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto.setVirtualMargin(entity.getVirtualMargin());
		dto.setRealMargin(entity.getRealMargin());
		dto.setUpdateUserId(entity.getUpdateUserId());
		dto.setAccessCardNo(entity.getAccessCardNo());
		dto.setRecharge(entity.getRecharge());
		mDao.updateMarginInfoByAccessCardNo(dto);
	}
	
	/**
	 * 积分卡更新
	 * @param memId
	 * @throws TmsDaoException 
	 */
	public void updateIntegralcard(Attn attn) throws TmsDaoException{
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto.setAccessCardId(attn.getAccessCardId());
		dto.setAccessCardNo(attn.getAccessCardNo());
		dto.setAttnId(attn.getAttnId());
		dto.setUpdateUserId(attn.getUpdateUserId());
		uDao.updateIntegralcard(dto);
	}
	
	/**
	 * 根据经办人ID 载入经办人
	 * @param attnId
	 * @return
	 * @throws TmsDaoException
	 */
	public Attn findAttnByAttnId(String attnId)throws TmsDaoException{
		Attn attn = Attn.getInstance();
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto = uDao.findAttnByAttnId(attnId);
		attn = dtoToEntity(dto,attn);
		return attn;
	}
	
	
	/**
	 * 根据userID 载入经办人
	 * @param userId
	 * @return
	 * @throws TmsDaoException
	 */
	public Attn findAttnByUserId(String userId)throws TmsDaoException{
		Attn attn = Attn.getInstance();
		AttnDao uDao = (AttnDao) TmsDao.getInstance(TmsDao.TMS_ATTN);
		AttnDto dto = new AttnDto();
		dto = uDao.findAttnByUserId(userId);
		attn = dtoToEntity(dto,attn);
		return attn;
	}
	
    private int totalDailyRemain;//全部用户余额
    private String currentDate;
    private int no;
    private int count;
    private int sumDailyRemain;//余额汇总
    private List<Attn> info;
    
    /**
     * 二、余额汇总表
     */
	public Attn finAttnList(String memberId) throws TmsDaoException {
		AttnDao aDao = (AttnDao)TmsDao.getInstance(TmsDao.TMS_ATTN);
		int totalDailyRemain = 0 ;
		List<AttnDto> all = aDao.findAllAttn();
		for (AttnDto attnDto : all) totalDailyRemain = totalDailyRemain + attnDto.getVirtualMargin();
		
		//
		List<AttnDto> attnList = aDao.findAttnList(memberId);
		int i = 1;
		int sumDailyRemain = 0;
		List<Attn> list = new ArrayList<Attn>();
		for (AttnDto attnDto : attnList) {
			Attn a = new Attn();
			a = dtoToEntity(attnDto, a);
			a.setNo(i++);
			if(i%2 == 0) a.setCssFlag(1);
			else a.setCssFlag(2);
			sumDailyRemain = sumDailyRemain + attnDto.getVirtualMargin();
			list.add(a);
		}

		//
		Attn attn = new Attn();
		attn.setInfo(list);
		attn.setSumDailyRemain(sumDailyRemain);
		attn.setTotalDailyRemain(totalDailyRemain);
		attn.setCount(all.size());
		attn.setCurrentDate(DateUtil.getCurrentDate());
		return attn;
	}
	
	/**
	 * 
	 */
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAttnId() {
		return attnId;
	}

	public void setAttnId(String attnId) {
		this.attnId = attnId;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public String getAttnName() {
		return attnName;
	}

	public void setAttnName(String attnName) {
		this.attnName = attnName;
	}

	public String getAttnIdCardNo() {
		return attnIdCardNo;
	}

	public void setAttnIdCardNo(String attnIdCardNo) {
		this.attnIdCardNo = attnIdCardNo;
	}

	public int getSumDailyRemain() {
		return sumDailyRemain;
	}

	public void setSumDailyRemain(int sumDailyRemain) {
		this.sumDailyRemain = sumDailyRemain;
	}

	public String getAttnAddress() {
		return attnAddress;
	}

	public void setAttnAddress(String attnAddress) {
		this.attnAddress = attnAddress;
	}

	public String getAttnPhone() {
		return attnPhone;
	}

	public void setAttnPhone(String attnPhone) {
		this.attnPhone = attnPhone;
	}
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getAttnPhoto() {
		return attnPhoto;
	}

	public void setAttnPhoto(String attnPhoto) {
		this.attnPhoto = attnPhoto;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public Timestamp getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(Timestamp insertTime) {
		this.insertTime = insertTime;
	}

	public int getUpdateUserId() {
		return updateUserId;
	}

	public void setUpdateUserId(int updateUserId) {
		this.updateUserId = updateUserId;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public String getAccessCardId() {
		return accessCardId;
	}

	public void setAccessCardId(String accessCardId) {
		this.accessCardId = accessCardId;
	}

	public String getAccessCardNo() {
		return accessCardNo;
	}

	public void setAccessCardNo(String accessCardNo) {
		this.accessCardNo = accessCardNo;
	}

	public int getCardScore() {
		return cardScore;
	}

	public void setCardScore(int cardScore) {
		this.cardScore = cardScore;
	}

	
	
	public int getGiftMargin() {
		return giftMargin;
	}

	public void setGiftMargin(int giftMargin) {
		this.giftMargin = giftMargin;
	}

	public int getRealMargin() {
		return realMargin;
	}

	public void setRealMargin(int realMargin) {
		this.realMargin = realMargin;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getAttnStatus() {
		return attnStatus;
	}

	public void setAttnStatus(String attnStatus) {
		this.attnStatus = attnStatus;
	}

	public int getCssFlag() {
		return cssFlag;
	}

	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	public double getIntegralRatio() {
		return integralRatio;
	}

	public void setIntegralRatio(double integralRatio) {
		this.integralRatio = integralRatio;
	}

	public int getVirtualMargin() {
		return virtualMargin;
	}

	public void setVirtualMargin(int virtualMargin) {
		this.virtualMargin = virtualMargin;
	}

	public int getRecharge() {
		return recharge;
	}

	public void setRecharge(int recharge) {
		this.recharge = recharge;
	}

	public int getTotalDailyRemain() {
		return totalDailyRemain;
	}

	public void setTotalDailyRemain(int totalDailyRemain) {
		this.totalDailyRemain = totalDailyRemain;
	}

	public String getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(String currentDate) {
		this.currentDate = currentDate;
	}

	public List<Attn> getInfo() {
		return info;
	}

	public void setInfo(List<Attn> info) {
		this.info = info;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

    
}
